import pygame
import sys
import random

# 初始化pygame
pygame.init()

# 确保中文能正常显示
pygame.font.init()
font = pygame.font.SysFont(['SimHei', 'WenQuanYi Micro Hei', 'Heiti TC'], 36)

# 游戏窗口设置
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("简单小游戏")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# 玩家设置
player_size = 50
player_x = WIDTH // 2 - player_size // 2
player_y = HEIGHT - player_size - 10
player_speed = 5


# 敌人类
class Enemy:
    def __init__(self):
        self.size = 40
        self.x = random.randint(0, WIDTH - self.size)
        self.y = -self.size
        self.speed = random.randint(2, 4)
        self.color = random.choice([RED, GREEN, BLUE])

    def move(self):
        self.y += self.speed

    def draw(self):
        pygame.draw.rect(screen, self.color, (self.x, self.y, self.size, self.size))

    def is_off_screen(self):
        return self.y > HEIGHT

    def collides_with(self, player):
        return (self.x < player.x + player.size and
                self.x + self.size > player.x and
                self.y < player.y + player.size and
                self.y + self.size > player.y)


# 玩家类
class Player:
    def __init__(self):
        self.x = player_x
        self.y = player_y
        self.size = player_size
        self.speed = player_speed
        self.score = 0

    def move_left(self):
        if self.x > 0:
            self.x -= self.speed

    def move_right(self):
        if self.x < WIDTH - self.size:
            self.x += self.speed

    def draw(self):
        pygame.draw.rect(screen, WHITE, (self.x, self.y, self.size, self.size))

    def increase_score(self):
        self.score += 1


# 创建玩家
player = Player()

# 敌人数组
enemies = []
enemy_spawn_timer = 0
ENEMY_SPAWN_RATE = 60  # 每60帧生成一个敌人

# 游戏主循环
clock = pygame.time.Clock()
game_over = False

while not game_over:
    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # 键盘控制
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        player.move_left()
    if keys[pygame.K_RIGHT]:
        player.move_right()

    # 生成敌人
    enemy_spawn_timer += 1
    if enemy_spawn_timer >= ENEMY_SPAWN_RATE:
        enemies.append(Enemy())
        enemy_spawn_timer = 0

    # 移动和检测碰撞
    for enemy in enemies[:]:
        enemy.move()
        if enemy.is_off_screen():
            enemies.remove(enemy)
            player.increase_score()
        elif enemy.collides_with(player):
            game_over = True

    # 绘制游戏元素
    screen.fill(BLACK)
    player.draw()

    for enemy in enemies:
        enemy.draw()

    # 显示分数
    score_text = font.render(f"分数: {player.score}", True, WHITE)
    screen.blit(score_text, (10, 10))

    if game_over:
        game_over_text = font.render("游戏结束！按任意键退出", True, WHITE)
        text_rect = game_over_text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
        screen.blit(game_over_text, text_rect)
        pygame.display.flip()
        pygame.time.wait(2000)

        # 等待用户按键退出
        waiting = True
        while waiting:
            for event in pygame.event.get():
                if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
                    pygame.quit()
                    sys.exit()

    pygame.display.flip()
    clock.tick(60)  # 控制游戏帧率